// index.js
Page({
  data: {
    wordBooks: [
      // { name: '大学四级', count: 600, selected: false },
      // { name: '大学六级', count: 800, selected: false },
      // { name: '托福核心词汇', count: 1200, selected: false },
      // { name: '雅思词汇', count: 1500, selected: false },
      // { name: 'GRE词汇', count: 2000, selected: false },
      // { name: '商务英语', count: 1000, selected: false },
      // { name: '考研词汇', count: 5500, selected: false },
      // { name: '专四词汇', count: 3000, selected: false }
    ],
    currentWordBookName: '',
    hasSelected: false,
    selectedIcon: '',
    unselectedIcon: ''
  },
  
  onLoad: function() {
    this.getWordsBanks();
    // 初始化页面
    this.updateHasSelected();
  },
  
  // 获取词库列表
  getWordsBanks: function() {
    wx.cloud.callFunction({
      name: 'wordsFunctions',
      data: {
        type: 'getTagList'
      },
      success: (res) => {
        console.log(res)
        if (res.result.success) {
          const tagList = res.result.data
          let currentWordBookName = ''
          const indexPage = getCurrentPages()[0];
          if (indexPage) {
            currentWordBookName = indexPage.data.currentwordbook
          }
          /*
            tagList = [{
              title: "标题名",
              count: 100,
              tagid: "tagid"
            }]
            将tagList转换成
            [{
              id: tagid,
              name: title,
              count: count,
              selected: false
            }]
          */
          const wordBooks = tagList.map(tag => ({
            id: tag.tagid,
            name: tag.title,
            count: tag.count,
            // 当currentWordBookName=tag.tagid时，selected=true
            selected: currentWordBookName === tag.title
          }))
          this.setData({
            wordBooks: wordBooks
          })
        }
      }
    })
  },

  // 返回上一页
  goBack: function() {
    wx.navigateBack();
  },
  
  // 选择词库
  selectWordBook: function(e) {
    const index = e.currentTarget.dataset.index;
    const wordBooks = this.data.wordBooks;
    for (let i = 0; i < wordBooks.length; i++) {
      if (i === index) {
        wordBooks[i].selected = true;
      } else {
        wordBooks[i].selected = false;
      }
    }
    // console.log(wordBooks)
    
    this.setData({
        wordBooks: wordBooks
    }, () => {
      // console.log(this.data.wordBooks)
      this.updateHasSelected();
    });
  },
  
  // 更新是否有选中的词库
  updateHasSelected: function() {
    const hasSelected = this.data.wordBooks.some(book => book.selected);
    this.setData({ 
        hasSelected: hasSelected,
        selectedCount: this.data.wordBooks.filter(book => book.selected).length
    });
    // console.log(this.data.wordBooks)
  },
  
  // 添加到记忆本
  addToMemory: function() {
    if (this.data.selectedCount > 1) {
      wx.showToast({
          title: '只能选择一个词库',
          icon: 'none'
      });
      return;
    }
    
    const selectedBooks = this.data.wordBooks
      .filter(book => book.selected)
      .map(book => book.name);
    
    console.log(selectedBooks)
    
    wx.cloud.callFunction({
      name: 'userFunctions',
      data: {
        type: 'saveUserWordBook',
        wordBookName: selectedBooks[0]
      },
      success: (res) => {
        console.log(res)
        if (res.result.success) {
          wx.showToast({
            title: '添加成功',
            icon: 'success',
            duration: 2000
          });
          const memoryPage = getCurrentPages()[1];
          if (memoryPage) {
            memoryPage.setData({
              currentWordBookName: selectedBooks[0]
            })
          }
          const indexPage = getCurrentPages()[0];
          if (indexPage) {
            indexPage.setData({
              currentwordbook: selectedBooks[0]
            })
          }
          // 跳转回上一页
          wx.navigateBack({
            delta: 1
          })
        } else {
          wx.showToast({
            title: '添加失败',
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  }
});